package com.syf.mail;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * @author syf
 * @create 2022-01-29 11:39
 */
@Component
@Slf4j
public class QQMail {

    @Value("${spring.mail.username}")
    private String from;   //邮箱发送方的邮箱

    @Autowired
    private JavaMailSender javaMailSender;  //邮箱发送对象

    public void sendMessage(String to, String msg,String subject){
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject(subject);
        mailMessage.setText(msg);
        mailMessage.setTo(to);
        mailMessage.setFrom(from);

        try{
            javaMailSender.send(mailMessage);
            log.info("邮件已发送。");
        }catch (Exception e){
            log.error("邮件发送失败。" + e.getMessage());
        }
    }

}
